package cn.micai.base.collections;

import java.util.concurrent.CountDownLatch;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/11 16:18
 */
public class CountDownLatchMain {

    public static void main(String [] args) {

        try {
            CountDownLatch latch = new CountDownLatch(3);

            // Initializing three dependent thread i.e. UI, database and logging
            UIInitialization uiInitialization = new UIInitialization(latch);
            Thread uiThread = new Thread(uiInitialization);

            LoggingInitialization loggingInitialization = new LoggingInitialization(latch);
            Thread loggingThread = new Thread(loggingInitialization);

            DatabaseInitialization databaseInitialization = new DatabaseInitialization(latch);
            Thread databaseThread = new Thread(databaseInitialization);

            uiThread.start();
            loggingThread.start();
            databaseThread.start();

            // Main thread will wait until above threads get completed
            latch.await();

            System.out.println("Initialization has been completed, main thread can proceed now");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
